<?php


namespace app\admin\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Ptt extends Controller
{
    public function _initialize()
    {
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform", $platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }

    public function index(){
        //权限查询
        $common = new Common();
        $role_id = $common->power();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);
        $this->assign("role_id",$role_id);

        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        $where['ptt_id'] = ['neq',''];
        if(!empty($_GET['keyword'])){
            $where['title'] = ['like',"%".$_GET['keyword']."%"];
        }

        if(!empty($_GET['status'])){
            $where['status'] = $_GET['status'];
        }

        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_content")->where($where)->limit($start,$limit)->order("sort desc,id desc")->select();

                foreach ($list as $k=>$v){
                    $classifName = Db::name("admin_ptt")->where(['id'=>$v['ptt_id']])->value("name");
                    $list[$k]['classifName'] = $classifName;
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                    $v['thumb'] = empty($v['thumb']) ? "/static/images/nopic-107.png" : $v['thumb'];
                    $list[$k]['thumb'] = "<img src='".$v['thumb']."' style='max-height:50px;max-width:58px;'>";
                }

                $count = Db::name("admin_content")->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        return $this->fetch('index');
    }

    public function addLists(){
        $thisWechat = Session::get("thisWechat");
        $uniacid = $thisWechat['id'];
        //获取分类
        $classify = Db::name("admin_ptt")->where(['uniacid'=>$uniacid])->field("id,name")->select();
        $this->assign("classify", $classify);

        if(empty($_GET['id'])){
            return view('addLists');
        }else {
            $id = $_GET['id'];
            $where['id'] = $id;
            $detail = Db::name("admin_content")->where($where)->find();
            $this->assign("detail",$detail);
            return view('editLists');
        }
    }

    //添加修改文章操作
    public function doLists(){
        $common = new Common();
        Db::startTrans();
        try{
            if(empty($_POST['title'])){
                throw new Exception("请输入标题");
            }
            $data = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            if(!empty($_POST['id'])){
                $id = $_POST['id'];
                //修改操作
                $data['updatetime'] = time();
                $data['update_id'] = Session::get("id");
                $id = Db::name("admin_content")->where(['id'=>$id])->update($data);
                $log['remark'] = "用户【" . Session::get("user_name") . "】更新内容,内容id【".$id."】";
            }else{
                //添加操作
                $data['addtime'] = time();
                $data['add_id'] = Session::get("id");
                $id = Db::name("admin_content")->insertGetId($data);
                $log['remark'] = "用户【" . Session::get("user_name") . "】添加新内容,内容id【".$id."】";
            }
            $log['controller'] = "Ptt";
            $log['methods'] = "doLists";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
            Db::commit();
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
            Db::rollback();
        }
        return $black;
    }



    public function delLists(){
        $common = new Common();
        try {
            $id = $_POST['id'];
            $navList = Db::name("admin_content")->where(['id' => ['in', $id]])->field("id,title")->select();
            foreach ($navList as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除一键通【".$v['title']."】删除id【" . $v['id'] . "】";
                $log['controller'] = "Ptt";
                $log['methods'] = "delLists";
                $common->addLog($log);
            }
            Db::name("admin_content")->where(['id' => ['in', $id]])->delete();
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //删除
    public function delClassify(){
        $common = new Common();
        try {
            $id = $_POST['id'];
            $navList = Db::name("admin_ptt")->where(['id' => ['in', $id]])->field("id,name")->select();
            foreach ($navList as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除一键通分类【".$v['name']."】删除id【" . $v['id'] . "】";
                $log['controller'] = "Ptt";
                $log['methods'] = "delClassify";
                $common->addLog($log);
            }
            Db::name("admin_ptt")->where(['id' => ['in', $id]])->delete();
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //详细信息
    public function classifyDetail(){
        //审核状态
        $audit = Db::name("config_attribute")->where(['key' => "enable_status"])->value("value");
        $audit = explode("|", $audit);
        foreach ($audit as $k => $v) {
            $audit[$k + 1] = $v;
        }
        unset($audit[0]);
        $this->assign("audit",$audit);
        $this->assign("role_id",$_POST['role_id']);

        if(empty($_POST['id'])){
            return view('addClassify');
        }else {
            $id = $_POST['id'];
            $where['id'] = $id;
            $detail = Db::name("admin_ptt")->where($where)->find();
            $this->assign("detail",$detail);
            return view('editClassify');
        }
    }


    //添加修改操作
    public function doClassify(){
        $common = new Common();
        try {
            $rule = [
                ["name", "require", "分类名称不能为空"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            if (empty($value['id'])) {
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $id = Db::name("admin_ptt")->insertGetId($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】添加一键通分类【".$value['name']."】,关联id【" . $id . "】";
            } else {
                $id = $value['id'];
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                Db::name("admin_ptt")->update($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】更新一键通分类【".$value['name']."】的信息,关联id【" . $id . "】";
            }
            $log['controller'] = "Ptt";
            $log['methods'] = "doClassify";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //分类列表
    public function classify(){
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if(!empty($_GET['keyword'])){
            $where['name'] = ['like',"%".$_GET['keyword']."%"];
        }
        if(!empty($_GET['status'])){
            $where['status'] = $_GET['status'];
        }
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_ptt")->where($where)->limit($start,$limit)->order("sort desc,id desc")->select();
                foreach ($list as $k=>$v){
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $count = Db::name("admin_ptt")->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }else{
            //权限查询
            $common = new Common();
            $role_id = $_GET['role_id'];
            $powerList = $common->showpower($role_id);
            $this->assign("powerList",$powerList);
            $this->assign("role_id",$role_id);
            return $this->fetch('classify');
        }

    }


}